<?php


namespace paySdk\functions\postar;


use Exception;
use GuzzleHttp\Exception\GuzzleException;
use paySdk\core\BasePostar;

class Pay extends BasePostar
{
    /**
     * 获取银联用户标识
     * @return string
     * @throws Exception|GuzzleException
     */
    public function tag() {
        try {
            $this->signature();
            $resp = $this->callApi($this->union_pay_tag_url_path, "POST");
            $this->log("resp: " . $resp);
            return $resp;
        } catch (Exception $e) {
            $this->log("{$e->getFile()} - {$e->getLine()} - {$e->getMessage()}", "error");
            throw $e;
        }
    }

    /**
     * 统一下单
     * @return string
     * @throws Exception|GuzzleException
     */
    public function pay(): string
    {
        try {
            $this->signature();
            $resp = $this->callApi($this->pay_url_path, "POST");
            $this->log("resp: " . $resp);
            return $resp;
        } catch (Exception $e) {
            $this->log("{$e->getFile()} - {$e->getLine()} - {$e->getMessage()}", "error");
            throw $e;
        }
    }

    /**
     * 异步回调验签
     * @return bool
     * @throws Exception
     */
    public function notify(): bool
    {
        try {
            $this->verify();
            return true;
        } catch (Exception $e) {
            $this->log("{$e->getFile()} - {$e->getLine()} - {$e->getMessage()}", "error");
            throw new Exception("验签失败");
        }
    }
}